<template>
    <div class="wrap">
        <van-nav-bar @click-left="onClickLeft" left-arrow left-text="返回" title="" @click-right="onClickRight">
            <template #right>
                <span   class="add-desc">新增</span>
                <van-icon name="plus" size="16" />
            </template>
        </van-nav-bar>
   
        <div class="card-wrap" :key="index" v-for="(item,index) in dataList">
            <div class="top">
                <div class="village"><span style="display:inline-block;width:70px">小区：</span>{{item.communityName}}</div>
                <div class="loudong"><span style="margin:10px 0;display:inline-block;width:70px">楼栋：</span>{{item.buildNo}}</div>
                <div class="fangwu"><span style="display:inline-block;width:70px">房屋号：</span>{{item.houseNo}}</div>
            </div>
            <div class="bottom">
                <div @click="next(item)" class="edit">编辑</div>
                <div v-if="item.checkState==0" @click="delClick(item)" class="del">删除</div>
            </div>
            <div class="status">
                <van-button size="mini" v-if="item.checkState==1" type="primary"> {{item.show}}</van-button>
                <van-button size="mini" v-if="item.checkState==2" type="danger"> {{item.show}}</van-button>
                <van-button size="mini" v-if="item.checkState==0" type="info"> {{item.show}}</van-button>
            </div>
        </div>  
    </div>
</template>

<script>
import { Notify,Dialog,Toast } from 'vant';
import axios from 'axios'
import api from '../api/myvillage.js'
    export default {
        data: function () {
            return {
                dataList:[],
                hainfo:'',
                type:'',
                messaget:'',
                village:'',
                uploader:[],
                uploader1:[],
                uploader2:[],
                n_place:'',
                danPicker:false,
                fileList:[],
                fileList1:[],
                fileList2:[],
                brandPicker:false,
                bcolorPicker:false,
                ccolorPicker:false,
                ctypePicker:false,
                rightsPicker:false,
                villagePicker:false,
                onlinePicker:false,
                binPicker:false,
                zongPicker:false,
                mianPicker:false,
                villageList:[],               
                hunPicker:false,
                jiaoyuPicker:false,
                showCalendar:false,
                village:'',
                form:{
                    zzmm:'',
                    activationCode:'',
                    "openId": "",
                    "name": "",
                    "communityId": "",
                    "pictureId": "",
                    "idCard": "",
                    "phone": "",
                    "sex": "",
                    "age": '',
                    "building": "",
                    "room": ""
                }
            }
        },
        components: {
           
        },
        created(){
            this.getData()
            //this.getAccess()
            // let xiaoqu=this.$route.query.xiaoqu
            // this.form.village=xiaoqu
            // for(let i=0;i<xiaoquList.length;i++){
            //     if(xiaoqu==xiaoquList[i].type){
            //         document.title=xiaoquList[i].name+'小区信息录入'
            //     }
            // }
        },
        mounted() {
           
        },
        methods: {
            onClickLeft() {
               this.$router.go(-1)
            },
            onClickRight(){
               this.$router.push({path:'/addvillage'}) 
            },
            next(val){
                this.$router.push({path:'/addvillage',query:{id:val.id}})
                
            },
            getVillage(){
               let that=this 
               api.getVillage().then(e=>{
                   if(e.code==200){
                        that.villageList=e.data.map(item=>{
                            item.text=item.name
                            return item
                        })
                        
                   }
                   
                  
               }).catch(err=>{
                   console.log(err)
               })
            },
            getData(){
                let that=this
                api.getCommunity().then(e=>{
                    if(e.code==200){
                       
                        that.dataList=e.data.map(item=>{
                            //审核状态 0-未审核 1-审核通过 2-审核不通过
                            switch(Number(item.checkState)){
                                case 0:
                                    item.show="未审核"
                                    break;
                                case 1:
                                    item.show="已通过"
                                    break;
                                   
                                case 2:
                                    item.show="未通过"
                                    break;
                            }
                            return item
                        })
                       
                    }
                }).catch(err=>{
                     console.log(err.response)
                })
                
            },
            getAccess(){
                let url='http://api.weixin.qq.com/sns/oauth2/access_token?appid=wxf43b9ebf8b7e67cc&secret=SECRET&code=CODE&'+getQueryVariable('code')
                axios.get(url)
                .then(function (response) {
                    console.log(response);
                })
                .catch(function (error) {
                    console.log(error);
                });
            },
            getOpenID(){
                let that=this
                let code=getQueryVariable('code')
              
                api.getOpenID({code}).then(e=>{
                    openid=e.data.data.openId
                    
                    setCookie('openid',openid,123)
                    that.getInfo()
                }).catch(err=>{
                   console.log(err.response.data)
                })
            },

            onReadd: function(file) {
                let that = this;
                Toast.loading({
                    duration:0,
                    message: '上传中...',
                    forbidClick: true,
                });
                let formData = new FormData();
               
               if(file.length){
                    for(let i=0;i<file.length;i++){
                        formData.append('files', file[0].file);
                    }
               }else{
                  formData.append('file', file.file); 
               }
               
                let config = {
                    headers: {
                        Authorization,
                        'Content-Type': 'application/x-www-form-urlencoded'
                    }
                }
               let id=this.form.pictureId?this.form.pictureId:1234
                axios.post(BASE+`/picture/uploadImg/${id}`, formData, config).then(res => {
                    if(res.data.code===200){
                       
                    Toast.clear()
                       Notify({ type: 'success', message: '上传成功' });
                        that.form.pictureId=res.data.data.id
                        
                    }else{
                       Notify({ type: 'warning', message: '上传失败' });
                    }
                }).catch(err=>{
                    Toast.clear()
                    that.uploader1=[]
                    Notify({ type: 'warning', message: '上传失败' });
                    console.log(err)
                });
            },
           
           
           
          
            villageConfirm(val){
                this.form.communityId=val.id
                this.village=val.text
                this.villagePicker=false
            },
            update(){
               let that=this
                api.updateguanlian(that.form).then(e=>{
                Toast.clear()
                if(e.code===200){
                    Dialog.alert({
                        title: '提示',
                        message: '您的信息更新成功，请勿重复提交',
                    }).then(() => {
                        that.$router.push('/home')
                    });
                    Notify({ type: 'success', message: '录入成功' });
                }
            }).catch(err=>{
                Toast.clear()
                console.log()
            })
           },
          
           add(){
               let that=this
                api.add(that.form).then(e=>{
                Toast.clear()
                if(e.code===200){
                    Dialog.alert({
                        title: '提示',
                        message: '您的信息录入成功，请勿重复提交',
                    }).then(() => {
                        that.$router.push('/home')
                    });
                    Notify({ type: 'success', message: '录入成功' });
                }
            }).catch(err=>{
                Toast.clear()
                console.log()
            })
           },
           delClick(val){
               let that=this
               
                
                Dialog.confirm({
                    title: '确认删除？',
                    message: '',
                })
                .then(() => {
                    let that=this
                    Toast.loading({
                        duration:0,
                        message: '',
                        forbidClick: true,
                    });
                    api.delhouse({id:val.id,openId:openid}).then(e=>{
                    Toast.clear()
                    if(e.code===200){
                        // Dialog.alert({
                        //     title: '提示',
                        //     message: '您的信息录入成功，请勿重复提交',
                        // }).then(() => {
                        //     that.$router.push('/home')
                        // });
                        that.getData()
                        Notify({ type: 'success', message: '删除成功' });
                    }
                    }).catch(err=>{
                        Toast.clear()
                        console.log()
                    })
                    
                })
                .catch((err) => {
                    console.log(err)
                });

                
               
           }
        }
    }
</script>

<style scoped lang="scss">
@import "../common.scss";
.wrap{
    background:#ecf8ff;
    font-size:px2rem(12px)
} 
.card-wrap{
    position:relative;
    padding:10px;
    width:90%;
    margin:0 auto;
    border-radius:10px;
    margin-top:8px;
    background:#fff;
}
.status{
    position:absolute;
    right:10px;
    top:40%;
}
.bottom{
    display:flex;
    justify-content: flex-end;
}
.edit{
    margin-right:12px;
   
    font-size:px2rem(8px);
    
   
    color:blue;
   
}
.del{
     font-size:px2rem(8px);
    margin-right:12px;
   
    color:red;
}

</style>
